package br.inf.solutions.apresentacoes.ssp.control;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.ReasonTransferReserve;
import br.inf.solutions.apresentacoes.ssp.model.Square;
import br.inf.solutions.apresentacoes.ssp.model.SquarePostReason;
import br.inf.solutions.apresentacoes.ssp.model.basics.Reason;

@Service
@Scope("prototype")
public class ReasonTransferReserveController extends BaseController<ReasonTransferReserve> {

	public ReasonTransferReserveController() {
		super(ReasonTransferReserve.class);
	}
	
	public Return getPresentReason(){
		Reason reason = (Reason) (data.get("reason") == null ? entity.getReason() : data.get("reason")); 
		String sql = "FROM ReasonTransferReserve WHERE reason=" + reason.getId() + " AND active=true ORDER BY reason.name";
		data.put("sql", sql);
		return super.searchByHQL();
	}
	
	public Return addSquarePostReason(){
		Return ret = new Return(true);
		
		Square square = (Square) data.get("square");
		PostGraduation post = (PostGraduation) data.get("post");
		
		SquarePostReason item = new SquarePostReason(entity, square, post);

		entity.getSquarePostReason().add(item);
		
		return ret;
	}
	
	public Return removeSquarePostReason(){
		Return ret = new Return(true);
		SquarePostReason item = (SquarePostReason) data.get("selectedSquarePostReason");

		entity.getSquarePostReason().remove(item);
		return ret;
	}
	
	public Return listAllActivated() {
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE active = true AND reason.active=true ORDER BY reason.name";
		data.put("sql", sql);
		return super.searchByHQL();
	}

}
